home *** CD-ROM | disk | FTP | other *** search
/ NeXT Education Software Sampler 1992 Fall / NeXT Education Software Sampler 1992 Fall.iso / Programming / Source / Adroff2 / adroff.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-05-16  |  10.6 KB  |  473 lines

  1. //  Modified for NeXT by J. Rettenmayer   5/16/91
  2. /*
  3. $Header: c:/res/adroff/RCS/adroff.c 1.9 88/09/07 07:56:03 jr Exp $
  4. */
  5.  
  6. /* Idea for action diagram formatter originated by John W. Rettenmayer,
  7.    who has guided the design and also contributed some code.
  8.  
  9.    Spring Quarter '86 --  ACTION DIAGRAMMER first programmed by Joyce Miller,
  10.                           Rhonda Aubrey, and Rob Romsa at
  11.               Eastern Montana College
  12.  
  13.    Spring Quarter '87 -- Line wrap and line continuation features added
  14.                  by Don Joppa at Eastern Montana College
  15.  
  16.    Summer 1988 --------  Bugs fixed and graphics characters added by Darin
  17.                          LeBleu at Northeast Louisiana University
  18. */
  19.  
  20. #include <stdio.h>
  21. #include <math.h>
  22. #include <ctype.h>
  23.  
  24. #define EQ ==
  25.  
  26. #define DOUHORG 205
  27. #define DOULOWCORG 200
  28. #define DOUUPPCORG 201
  29. #define DOUVERG 186
  30. #define EQUALA 61
  31. #define FALSE 0
  32. #define INDENT 5
  33. #define LNLGTH 512
  34. #define NSIZE 15
  35. #define PLUS 43
  36. #define PRINTLNLGTH 80
  37. #define RIGHT_MARGIN 7
  38. #define SINHORG 196
  39. #define SINLOWCORG 192
  40. #define SINLOWLEFCOR 192
  41. #define SINLOWRIGCOR 217
  42. #define SINUPPCORG 218
  43. #define SINUPPRIGCOR 191
  44. #define SINVERA 124
  45. #define SINVERCONA 124
  46. #define SINVERCONG 195
  47. #define SINVERG 179
  48. #define SLASHA 45
  49. #define ARROW 60
  50. #define TRUE 1
  51.  
  52. int     firstcol = 10;
  53. int     indwidth = 5;
  54. int     scrwidth = 80;
  55.  
  56. FILE * infile;
  57. FILE * outfile;
  58.  
  59. main (argc, argv)
  60. int     argc;
  61. char   *argv[];
  62. {
  63.     int     i,
  64.         test,
  65.             numbars = 0,
  66.             g,
  67.             barchar = 0,
  68.         fl = 'm';
  69.     char    line[LNLGTH];
  70.  
  71.     g = openfiles (argc, argv);
  72.  
  73.     while (getstring (line) !=  0) {
  74.     test = FALSE;
  75.     if (line[0] EQ '.') {
  76.         test = TRUE;
  77.         execute (line, &numbars, g);
  78.         if (line[1] != 'b' && line[1] != 'i' && line[1] != 'g') {
  79.         getstring (line);
  80.         if (line[0] EQ '.' && line[1] EQ 'b')
  81.           drawbox (numbars, g);
  82.         else
  83.           print_line (line, numbars, test);
  84.         }
  85.     }
  86.     else {
  87.         printbars (numbars, barchar, fl);
  88.         for (i = 0; i < indwidth; i++)
  89.         fprintf (outfile, " ");
  90.         print_line (line, numbars, test);
  91.     }
  92.     }
  93. }
  94.  
  95.  
  96. execute (line, numbars, g)
  97. char   *line;
  98. int    *numbars;
  99. int    g;
  100.  
  101. {
  102.  
  103.     char    command,
  104.             ext,
  105.             fl = 'e';
  106.     int        barchar,
  107.             n;
  108.     static  int     sv = SINVERA,
  109.             svc = SINVERCONA,
  110.             dvr = SINVERA,
  111.             shr = SINVERA,
  112.             dhl = SINVERA,
  113.             shl = SINVERA,
  114.             dhr = SINVERA,
  115.                  sh = SLASHA,
  116.             dh = SLASHA,
  117.                  dhh = EQUALA;
  118.     if (g == 1) sv = SINVERG,
  119.         svc = SINVERCONG,
  120.         dvr = DOUVERG,
  121.         shr = SINUPPCORG,
  122.         dhl = DOULOWCORG,
  123.         shl = SINLOWCORG,
  124.         dhr = DOUUPPCORG,
  125.         sh = SINHORG,
  126.         dh = DOUHORG,
  127.         dhh = DOUHORG;
  128.  
  129.  
  130.     ext = (strlen (line) > 2) ? line[2] : '1';
  131.     command = line[1];
  132.     barchar = 0;
  133.  
  134.    switch (command) {
  135.     case 'i':
  136.         barchar = sv;
  137.         inoutput (*numbars, barchar, fl, ext);
  138.         break;
  139.     case 'b':
  140.         barchar = sv;
  141.         printbars (*numbars, barchar, fl);
  142.         drawbox (*numbars, g);
  143.         break;
  144.     case 't':
  145.         printarrows (*numbars, ext, g);
  146.         break;
  147.     case 'p':
  148.         fprintf (outfile, "\f\n");
  149.         barchar = sv;
  150.         printbars (*numbars, barchar, fl);
  151.         break;
  152.     case 's':                   /* selection command         */
  153.     case 'c':                   /* original "choice" command */
  154.         barchar = sv;
  155.         printbars (*numbars, barchar, fl);
  156.         fprintf (outfile, "%c%c%c%c%c", shr, sh, sh, sh, ' ');
  157.         (*numbars)++;
  158.         break;
  159.  
  160.     case '-':
  161.          barchar = sv;
  162.         printbars (*numbars - 1, barchar, fl);
  163.             fprintf (outfile, "%c%c%c%c%c", svc, sh, sh, sh, ' ');
  164.         break;
  165.  
  166.     case 'e':
  167.         (*numbars)--;
  168.         n = printbars (*numbars);
  169.         if (n == 186)
  170.         fprintf (outfile, "%c%c%c%c%c", dhl, dh, dh, dh, ' ');
  171.         else
  172.         fprintf (outfile, "%c%c%c%c%c", shl, sh, sh, sh, ' ');
  173.         break;
  174.  
  175.     case 'r':
  176.         barchar = dvr;
  177.         printbars (*numbars, barchar, fl);
  178.         fprintf (outfile, "%c%c%c%c%c", dhr, dhh, dhh, dhh, ' ');
  179.         (*numbars)++;
  180.         break;
  181.     default:
  182.         fprintf (stderr, "UNRECOGNIZABLE DOT COMMAND -- %c --\n\n", command)
  183. ;
  184.         fprintf (stderr, "PLEASE CORRECT DOT COMMAND\n");
  185.         exit (1);
  186.         break;
  187.     }
  188. }
  189.  
  190.  
  191. drawbox (numbars, g)
  192. int    *numbars,
  193.        g;
  194. {
  195.  
  196.     char    line[LNLGTH];
  197.     int     i,
  198.         fl = 'b',
  199.         barchar = 0,
  200.             dc1 = PLUS,
  201.         dc2 = PLUS,
  202.         dc3 = PLUS,
  203.         dc4 = PLUS,
  204.         s = SLASHA,
  205.         l = SINVERA;
  206.  
  207.     if (g == 1)
  208.         dc1 = SINUPPCORG,
  209.         dc2 = SINUPPRIGCOR,
  210.         dc3 = SINLOWLEFCOR,
  211.         dc4 = SINLOWRIGCOR,
  212.         s =   SINHORG,
  213.         l =   SINVERG;
  214.     getstring (line);
  215.     if (line[strlen (line) - 1] EQ '\n')
  216.     line[strlen (line) - 1] = '\0';
  217.     fprintf (outfile, "     %c", dc1);
  218.     for (i = 0; i < (strlen (line) + 2); i++)
  219.     fprintf (outfile, "%c", s);
  220.     fprintf (outfile, "%c\n", dc2);
  221.     printbars (numbars, barchar, fl);
  222.     fprintf (outfile, "     %c ", l);
  223.     fprintf (outfile, "%s", line);
  224.     fprintf (outfile, " %c\n", l);
  225.     printbars (numbars);
  226.     fprintf (outfile, "     %c", dc3);
  227.     for (i = 0; i < (strlen (line) + 2); i++)
  228.     fprintf (outfile, "%c", s);
  229.     fprintf (outfile, "%c\n", dc4);
  230. }
  231. inoutput (numbars, barchar, fl, ext)
  232. int     numbars;
  233. int     barchar;
  234. char    fl;
  235. char    ext;
  236. {
  237.     char    line[LNLGTH];
  238.     int     i, j,
  239.             fill;
  240.     int     extension;
  241.  
  242.     extension = ext - '0';
  243.  
  244.     for (i = 0; i < extension; i++) {
  245.     printbars (numbars, barchar, fl);
  246.     getstring (line);
  247.     fill = scrwidth - ((2 * (indwidth * numbars)) + strlen (line) + 5);
  248.     for (j = 0; j < fill; j++) {
  249.         fprintf (outfile, " ");
  250.     }
  251.     fprintf (outfile, "*%s", line);
  252.     }
  253. }
  254. printarrows (numbars, ext, g)
  255. int     numbars;
  256. char    ext;
  257. int g;
  258. {
  259.     int     i, num, extension;
  260.     int numext,
  261.     fl = 'a',
  262.         barchar = 0,
  263.         s = SLASHA,
  264.     ar = ARROW;
  265.  
  266.     if (g == 1)
  267.         s = SINHORG,
  268.         ar = ARROW;
  269.  
  270.     extension = ext - '0';
  271.  
  272.     if (extension == numbars) {
  273.     for (i = 1; i < (firstcol); i++)
  274.         fprintf (outfile, " ");
  275.     }
  276.     else {
  277.     numext = (numbars - extension);
  278.     printbars (numext, barchar, fl);
  279.     }
  280.     fprintf (outfile, "\b%c", ar);
  281.     for (i = 0; i < extension; i++)
  282.     fprintf (outfile, "%c%c%c%c%c", s, s, s, s, s);
  283.     fprintf (outfile, "%c%c%c%c ", s, s, s, s);
  284. }
  285.  
  286.  
  287. printbars (numbars, barchar, fl)
  288. int     numbars;
  289. int     barchar;
  290. char    fl;
  291. {
  292. static    int    bartable[NSIZE];
  293.     int     i,
  294.             j,
  295.         x;
  296.     bartable[0] = 179;
  297.     if (fl == 'e') bartable[numbars + 1] = barchar;
  298.     for (i = 1; i < firstcol; i++)
  299.     fprintf (outfile, " ");
  300.     for (i = 1; i <= numbars; i++) {
  301.     fprintf (outfile, "%c", bartable[i]);
  302.     for (j = 1; j < indwidth; j++)
  303.         fprintf (outfile, " ");
  304.     }
  305.     return(bartable[i]);
  306. }
  307.  
  308. openfiles (argc, argv)
  309. int     argc;
  310. char   *argv[];
  311. {
  312.   int hadopt = 0;
  313.  
  314.   static char rcsrev[] = "This is $Revision: 1.9 $\n";
  315.   int i;
  316.  
  317.   infile = stdin;
  318.   outfile = stdout;
  319.  
  320.   if (argc > 1) {
  321.     if (strncmp (argv[1], "-", 1) == 0){
  322.     if (strncmp (argv[1], "-g", 2) == 0)
  323.         hadopt = 1;
  324.         if (strncmp (argv[1], "-v", 2) == 0){
  325.         printf("%s", rcsrev);
  326.         exit(1);
  327.     }
  328.     }
  329.     switch (argc) {
  330.       case 2:
  331.     if (hadopt)
  332.       /*  fprintf (stderr, "No Input Given")  */ ;
  333.     else
  334.       if ((infile = fopen (argv[1], "r")) EQ NULL)
  335.           printerrmsg (argv[1]);
  336.         break;
  337.       case 3:
  338.     if (hadopt) {
  339.       if ((infile = fopen (argv[2], "r")) EQ NULL)
  340.          printerrmsg (argv[2]);
  341.          }
  342.       else{
  343.         if ((infile = fopen (argv[1], "r")) EQ NULL)
  344.           printerrmsg (argv[1]);
  345.         if ((outfile = fopen (argv[2], "w")) EQ NULL)
  346.           printerrmsg (argv[2]);
  347.       }
  348.       break;
  349.       case 4:
  350.     if (hadopt){
  351.       if ((infile = fopen (argv[2], "r")) EQ NULL)
  352.          printerrmsg (argv[2]);
  353.       if ((outfile = fopen (argv[3], "w")) EQ NULL)
  354.          printerrmsg (argv[3]);
  355.     }
  356.         else{
  357.       fprintf(stderr, "Too many arguments to adroff");
  358.       exit (1);
  359.     }
  360.       break;
  361.       default:
  362.     fprintf (stderr, "Too many arguments to adroff");
  363.     exit (1);
  364.     break;
  365.     }
  366.   }
  367.   return (hadopt);
  368. }
  369.  
  370. printerrmsg (filename)
  371. char   *filename;
  372. {
  373.     fprintf (stderr, "Can not open %s\n", filename);
  374.     exit (1);
  375. }
  376.  
  377.  
  378. print_line (line, numbars, test)
  379. char   *line;
  380. int     numbars, test;
  381. {
  382.     char    fl = 'p';
  383.     int     barchar = 0,
  384.         counter = 0,
  385.             i,
  386.             length_of_line = strlen (line),
  387.             k = 0;
  388.  
  389.     break_line_up (numbars, line, test);
  390.     while (counter <= length_of_line - 1) {
  391.     k++;
  392.     if (k != 1) {
  393.         fputc ('\n', outfile);
  394.         printbars (numbars, barchar, fl);
  395.         if (test == TRUE)
  396.         for (i = 1; i <= INDENT; i++)
  397.             fputc (' ', outfile);
  398.             else
  399.             for (i = 1; i <= (indwidth + INDENT); i++)
  400.                 fputc (' ', outfile);
  401.         counter++;
  402.     }
  403.     while ((line[counter] != '\0') && (counter <= length_of_line - 1))
  404.         fputc (line[counter++], outfile);
  405.     }
  406. }
  407.  
  408. break_line_up (numbars, line, test)
  409. int     numbars,
  410.     test;
  411. char   *line;
  412.  
  413. {
  414.     int     k = 1,
  415.             out_line_length,
  416.             spaces_used,
  417.             pointer,
  418.             length_of_str = strlen (line),
  419.             counter;
  420.  
  421.     spaces_used = firstcol + numbars * indwidth - 1;
  422.     out_line_length = PRINTLNLGTH - spaces_used - RIGHT_MARGIN;
  423.     pointer = out_line_length;
  424.     if (test == TRUE)
  425.     out_line_length = out_line_length + 5;
  426.     if (out_line_length < length_of_str) {
  427.     while (pointer <= length_of_str - 3) {
  428.         counter = 0;
  429.         if (k == 2)
  430.                out_line_length -= INDENT;
  431.         while (!isspace (line[pointer - counter]))
  432.         counter++;
  433.         line[pointer - counter] = '\0';
  434.         pointer += (out_line_length - counter);
  435.         k++;
  436.     }
  437.     }
  438. }
  439.  
  440.  
  441. int getstring (char * line)
  442.  
  443. {
  444.     char    line2[LNLGTH];
  445.     int       place_of_alpha = 2;
  446.  
  447.     if (fgets (line, LNLGTH, infile) != NULL) {
  448.     place_of_alpha = find_alpha(line, place_of_alpha);
  449.     while (line[strlen (line) - place_of_alpha] == '\\') {
  450.         if (isspace(line[strlen (line) - (place_of_alpha + 1)])) {
  451.         place_of_alpha = find_alpha(line,
  452.             place_of_alpha + 1);
  453.         place_of_alpha--;
  454.         }
  455.         fgets (line2, LNLGTH, infile);
  456.         line[strlen (line) - (place_of_alpha)] = ' ';
  457.         line[strlen (line) - (place_of_alpha - 1)] = '\0';
  458.         strcat (line, line2);
  459.         place_of_alpha = 2;
  460.         place_of_alpha = find_alpha(line,
  461.             place_of_alpha);
  462.     }
  463.     }
  464.     else
  465.     return (0);
  466. }
  467.  
  468. find_alpha (char * line, int counter)
  469. {
  470.     while (isspace (line[strlen (line) - counter]))
  471.     counter++;
  472.     return (counter);
  473. }
  474.